package com.bms.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bms.bean.User;
import com.bms.service.UserService;
import com.bms.tools.EncryptionTool;
import com.bms.tools.MailSender;

@Controller
public class CommonController extends BasicController {
	
	@Resource
	private MailSender msender;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("/login.htm")
	public String addUser(){
		return "/login";
	}
	
	@RequestMapping("/access-denied.htm")
	public String accessDenied(){
		return "/access-denied-page";
	}
	
	@RequestMapping("/regist.htm")
	public String regist(){
		return "/regist-page";
	}
	
	@RequestMapping("/regist_sendmail.htm")
	public String sendMail(HttpServletRequest request) throws Exception{
		String email    	   = request.getParameter("email");
		String password 	   = request.getParameter("password");
		String confirmPassword = request.getParameter("confirmPassword");
		
		password = EncryptionTool.encryptBMSPassWord(password.getBytes());
		long userId = userService.addUser(null, "", "", password, new Date(), email, 0, 0, 3);
		
		if(userId>0){
			String html = getContentHTML(email,userId);
			msender.sendEMail(email, "欢迎注册，请验证邮箱登录", html);
		}
		return "/regist-sendemail";
	}
	
	@RequestMapping("/regist_checkmail.htm")
	public String checkMail(String code,Model model) throws Exception{
		userService.updateUserNotValidate(code, 1, "", "", null, 0, "");
		model.addAttribute("userId", code);
		return "/finish-information";
	}
	
	/**
	 * 完善个人资料
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/regist_finishinformation.htm")
	public String finishInformation(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userId     = request.getParameter("userId");
		String nickName   = request.getParameter("nickName");
		String userSex    = request.getParameter("userSex");
		String mobileNo   = request.getParameter("mobileNo");
		String birthDay   = request.getParameter("birthDay");
		String userPic300 = request.getParameter("userPic300");
		String userPic30 = request.getParameter("userPic30");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date birthDayDate = sdf.parse(birthDay);
		
		userService.updateUser(userId, 1, nickName, mobileNo, birthDayDate, Integer.valueOf("".equals(userSex)?"0":userSex), userPic300,userPic30);
		
		return "redirect:/j_spring_security_check";
	}
	
	@RequestMapping("/regist_test.htm")
	public String test() throws Exception{
		return "/finish-information";
	}
	
	
	public String getContentHTML(String email,long userId) throws Exception{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String todayDate = sdf.format(date);
		
//		String userIdBase64 = EncryptionTool.encryptBASE64(String.valueOf(userId).getBytes());
		String html = "<html><head><base target='_blank'>"
				+ "<style type='text/css'>"
				+ "::-webkit-scrollbar{ display: none; }"
				+ "</style>"
				+ "<style id='cloudAttachStyle' type='text/css'>"
				+ "#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}"
				+ "</style>"
				+ "</head><body><table cellpadding='0' cellspacing='0' border='0' width='640' style='margin:0 auto;color:#555; font:16px/26px '微软雅黑','宋体',Arail; '>"
				+ "<tbody>"
				+ "<tr style='background-color:#fff;'>"
				+ "<td style='padding:10px 38px;'>"
				+ "<div style='margin:20px 0;'>"
				+ " 欢迎加入BMS"
				+ "<br> 你的登录邮箱是："
				+ "<span style='color:#019875;'><a href='mailto:"+email+"' target='_blank'>"+email+"</a></span>"
				+ "</div> 请点击以下链接验证你的邮箱地址，验证后就可以使用BMS的所有功能啦! <br>"
				+ "<div style=' word-break:break-all;word-wrap:break-word;'>"
				+ "<a href='http://localhost:8080/regist_checkmail.htm?code="+userId+"' target='_blank' style='color:#019875; text-decoration:underline;'>http://localhost:8080/regist_checkmail.htm?code="+userId+"</a>"
				+ "<br>"
				+ "</div>"
				+ "<div style='margin:5px 0 20px 0; font-size:13px;'>"
				+ "如果以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中。"
				+ "</div>"
				+ "<div>"
				+ "  祝职业更上一层楼！"
				+ "</div>"
				+ "<div style='margin:20px 0;'>"
				+ "BMS团队"
				+ "<br> "+todayDate
				+ "</div>"
				+ "</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td style='line-height:30px;text-align:right;font-size:14px;'> 为保证邮箱正常接收，<a href='mailto:cyj_mrcool@163.com'>请将cyj_mrcool@163.com添加进你的通讯录</a></td>"
				+ "</tr>"
				+ "</tbody>"
				+ "</table>"
				+ "<img id='1434099802394_Easeye_isOpen' src='http://linktrace.lagou.com/MailLink/LogoImageHandler.jpg?EASEYEUID=N132-GYYTMMY-GE2TANRRGIYTOMBTGIZDSOJZGAZDG-MN4WUX3NOJRW633MIAYTMMZOMNXW2-19F32459&amp;' height='1' width='1'>"

				+ "<style type='text/css'>"
				+ "body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}"
				+ "td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}"
				+ "pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}"
				+ "th,td{font-family:arial,verdana,sans-serif;line-height:1.666}"
				+ "img{ border:0}"
				+ "header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}"
				+ "</style>"

				+ "<style id='ntes_link_color' type='text/css'>a,td a{color:#064977}</style>"
				+ "</body></html>";
		
		return html;
	}
}
